package com.example.ending_demo.servlet;

import com.example.ending_demo.dao.UserDao;
import com.example.ending_demo.model.User;

import java.io.*;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import java.io.IOException;

@WebServlet(urlPatterns = "/LoginServlet")
public class LoginServlet extends HttpServlet {

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


        /*
        1. 读取Cookie列表信息，判断有没有用户名和密码的Cookie，
        如果有，从Cookie中获取用户名和密码，执行第2步，
        否则从请求参数中获取用户名和密码，执行第2步；
         */
        Cookie [] cookies = request.getCookies();
        String username = null;
        String password = null;

        if(cookies!=null){
            for(Cookie cookie:cookies){
                if(cookie.getName().equals("usernmae")){
                    username = cookie.getValue();
                }else if (cookie.getName().equals("passowrd")){
                    password = cookie.getValue();
                }
            }
        }

        if(username==null||password==null) {
            username = request.getParameter("username");
            password = request.getParameter("password");
        }
        /*
        2. 验证用户名和密码；
        */
        UserDao userDao = new UserDao();
        User user = new User(username,password);
        boolean isRight = userDao.findUser(user);
        String login = request.getParameter("login");
        if(isRight){
            user = userDao.findUserInfo(user);
            Cookie usernameCookie;
            Cookie passwordCookie;

            //如果用户勾选了自动登录，创建Cookie
            if(login!=null&&login.equals("yes")){
                //用Cookie保存用户的账号和密码
                usernameCookie = new Cookie("username",username);
                passwordCookie = new Cookie("password",password);

                //设置Cookie的时间为60秒
                usernameCookie.setMaxAge(60);
                passwordCookie.setMaxAge(60);

                response.addCookie(usernameCookie);
                response.addCookie(passwordCookie);

                HttpSession session = request.getSession();
                session.setAttribute("user",user);
            }else {
                //如果没有勾选就删除Cookie
                usernameCookie = new Cookie("username",username);
                passwordCookie = new Cookie("password",password);

                usernameCookie.setMaxAge(0);
                passwordCookie.setMaxAge(0);

                response.addCookie(usernameCookie);
                response.addCookie(passwordCookie);

                HttpSession session = request.getSession();
                session.setAttribute("user",user);
            }
            response.sendRedirect("index.jsp");
        }else{
            response.sendRedirect("login.jsp");
        }


    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doPost(request,response);
    }
}